package com.andova.toolkit.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by Administrator on 2018-05-11.
 *
 * @author kzaxil
 * @since 1.0.0
 */
public abstract class MultiRecyclerAdapter<T> extends RecyclerView.Adapter<MultiRecyclerAdapter.RecyclerViewHolder> {
    private List<T> mDataModelList = new ArrayList<>();

    public void add(T model) {
        if (model == null) return;
        mDataModelList.add(model);
    }

    public void add(List<T> list) {
        if (list == null || list.size() == 0) return;
        mDataModelList.addAll(list);
    }

    public void list(List<T> list) {
        if (list == null || list.size() == 0) return;
        mDataModelList = list;
    }

    public void remove(T model) {
        mDataModelList.remove(model);
    }

    public List<T> list() {
        return mDataModelList;
    }

    public void clear() {
        mDataModelList.clear();
    }

    @NonNull
    @Override
    public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new RecyclerViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(onCreateViewHolder().get(viewType), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
        T model = mDataModelList.get(position);
        execute(position, getItemViewType(position), holder.itemView, model);
    }

    @Override
    public int getItemCount() {
        return mDataModelList.size();
    }

    @Override
    public int getItemViewType(int position) {
        T model = mDataModelList.get(position);
        return getItemViewType(position, model);
    }

    static class RecyclerViewHolder extends RecyclerView.ViewHolder {
        RecyclerViewHolder(View itemView) {
            super(itemView);
        }
    }

    public abstract int getItemViewType(int position, T model);

    @NonNull
    public abstract Map<Integer, Integer> onCreateViewHolder();

    public abstract void execute(int position, int type, View itemView, T model);
}
